package com.fw.ui.view.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.vaadin.server.VaadinService;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class ApplicationLogoutButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private PropertyBAO propertyBAO;

    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	SecurityContextHolder.getContext().setAuthentication(null);
	ApplicationUI applicationUI = (ApplicationUI) VaadinService.getCurrent().findUI(VaadinService.getCurrentRequest());
	String landing = propertyBAO.getPropertyValue("common.landing.page");
	applicationUI.getNavigator().navigateTo(landing);
	applicationUI.buildLoginView(true);
	return null;
    }

    @Override
    public boolean validate(ClickEvent event) throws Exception {
	return true;
    }

}
